﻿using MediatR;
using WarehouseManagement.Application.Command.WarehouseSAppCommand;
using WarehouseManagement.Domain.DTO.WarehouseSApp;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.WarehouseSAppCommandHandler
{
    /// <summary>
    /// 入库申请反填中介者
    /// </summary>
    public class WarehouseSAppFanCommandHandler : IRequestHandler<WarehouseSAppFanCommand, FanWarehouseSAppDto>
    {
        private readonly lnterfaceWarehouseSAppRepository _warehouseSAppRepository;//仓库入库申请表
        private readonly lnterfaceStorageProductRepository _storageProductsRepository;//仓库入库申请产品明细表

        public WarehouseSAppFanCommandHandler(lnterfaceWarehouseSAppRepository warehouseSAppRepository, lnterfaceStorageProductRepository storageProductsRepository)
        {
            _warehouseSAppRepository = warehouseSAppRepository;
            _storageProductsRepository = storageProductsRepository;
        }

        public async Task<FanWarehouseSAppDto> Handle(WarehouseSAppFanCommand request, CancellationToken cancellationToken)
        {
            var res = (await _warehouseSAppRepository.GetAll()).First(x => x.ShippRequestAppId == request.id);
            FanWarehouseSAppDto dto = new FanWarehouseSAppDto();
            dto.ShippRequestAppId = res.ShippRequestAppId;
            dto.StorageTopic = res.StorageTopic;
            dto.StorageNumber = res.StorageNumber;
            dto.StorageSource = res.StorageSource;
            dto.WName = res.WName;
            dto.AppName = res.AppName;
            dto.AppDate = res.AppDate;
            dto.StorageType = res.StorageType;
            dto.VName = res.VName;
            dto.StorageRemark = res.StorageRemark;
            dto.State=res.State;
            dto.AddName = res.AddName;
            dto.AddTime = res.AddTime;
            dto.Office_Id = res.Office_Id;
            dto.User_Id = res.User_Id;
            var res1 = (await _storageProductsRepository.GetAll()).Where(x => x.StorageNumber == res.StorageNumber).Select(item => new StorageProductDto
            {
                StorageProductId = item.StorageProductId,
                StorageNumber = item.StorageNumber,
                PNumber = item.PNumber,
                PName = item.PName,
                PSpec = item.PSpec,
                PUnit = item.PUnit,
                PPrice = item.PPrice,
                SPConfirmNum = item.SPConfirmNum,
                SumPrice = item.SumPrice,
                Batch = item.Batch,
                CreateDate = item.CreateDate,
                ValidDate = item.ValidDate,
                ArriveDate = item.ArriveDate,
                WrapName = item.WrapName,
                Remark = item.Remark,
                SumNum = item.SumNum,
            }).ToList();

            dto.list = res1;
            return dto;


        }
            
        
    }
}
